/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = rtsp_comm.h                                        */
/*  NOTE      =                                                    */
/*  DATE      = 2015/07/14 by songbb                               */
/*******************************************************************/

#ifndef __RTSP_COMM_H_
#define __RTSP_COMM_H_

#include <sys/time.h>

#include "frlog.h"

#include "rtsp_core.h"

#define APM_RTSP_DBG 0
#define APM_RTSP_ENABLE_FRLOG 0

/* 标识符变量的初始化值 */
#define APM_RTSP_FLAG_UNSET -1

// 一行结束 end-of-line
#define APM_RTSP_EOL "\r\n"
// 头结束 end-of-header
#define APM_RTSP_EOH "\r\n\r\n"
// 头域分隔符
#define APM_RTSP_COLON ":"
// 空格
#define APM_RTSP_SP " "
// RTSP Version
#define APM_RTSP_VERSION "RTSP/1.0"

#define APM_RTSP_REPORT_MODULE_NAME "rtsp"
#define APM_RTSP_REPORT_REQ_METHOD APM_REPORT_KEY_REQ"."APM_RTSP_REPORT_MODULE_NAME".method"
#define APM_RTSP_REPORT_REQ_URI APM_REPORT_KEY_REQ"."APM_RTSP_REPORT_MODULE_NAME".uri"
#define APM_RTSP_REPORT_REQ_HDR_PREFIX APM_REPORT_KEY_REQ"."APM_RTSP_REPORT_MODULE_NAME".header."
#define APM_RTSP_REPORT_REQ_BODY APM_REPORT_KEY_REQ"."APM_RTSP_REPORT_MODULE_NAME".body"
#define APM_RTSP_REPORT_RESP_HDR_PREFIX APM_REPORT_KEY_RESP"."APM_RTSP_REPORT_MODULE_NAME".header."

#define APM_RTSP_METHOD_OPTIONS "OPTIONS"
#define APM_RTSP_METHOD_DESCRIBE "DESCRIBE"
#define APM_RTSP_METHOD_ANNOUNCE "ANNOUNCE"
#define APM_RTSP_METHOD_SETUP "SETUP"
#define APM_RTSP_METHOD_PLAY "PLAY"
#define APM_RTSP_METHOD_PAUSE "PAUSE"
#define APM_RTSP_METHOD_TEARDOWN "TEARDOWN"
#define APM_RTSP_METHOD_GET_PARAMETER "GET_PARAMETER"
#define APM_RTSP_METHOD_SET_PARAMETER "SET_PARAMETER"
#define APM_RTSP_METHOD_REDIRECT "REDIRECT"
#define APM_RTSP_METHOD_RECORD "RECORD"

#define KRED        "\x1B[0;31m"
#define KSALMON     "\x1B[1;31m"
#define KGREEN      "\x1B[0;32m"
#define KLIMEGREEN  "\x1B[1;32m"
#define KYELLOW     "\x1B[0;33m"
#define KORANGE     "\x1B[0;34m"
#define KBLUE       "\x1B[1;34m"
#define KMAGENTA    "\x1B[0;35m"
#define KPINK       "\x1B[1;35m"
#define KCYAN       "\x1B[0;36m"
#define RESET       "\x1B[0m"

#if APM_RTSP_DBG
    #define RTSP_PRINT(...) printf(__VA_ARGS__)
    #define RTSP_PRINT_DETAIL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(__VA_ARGS__))
    #define RTSP_PRINT_DBG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTSP_PRINT_FATAL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KORANGE),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTSP_PRINT_EMERG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KRED),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTSP_PRINT_ALERT(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KSALMON),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTSP_PRINT_CRIT(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KMAGENTA),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
    #define RTSP_PRINT_ERR(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KPINK),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTSP_PRINT_WARN(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KYELLOW),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTSP_PRINT_NOTICE(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KLIMEGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTSP_PRINT_INFO(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KBLUE),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
     #define PRINT_HEX_STRING(a,b,c) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),rtsp_print_hex_string(b,c),printf("]\n"),\
                            printf(RESET))
    #define RTSP_PRINT_PACKET_SPECIFIED(tag, packet, offset, len) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KORANGE),\
                            printf(tag":["),rtsp_print_packet_specified(packet,offset,len),printf("]\n"),\
                            printf(RESET))

    #define RTSP_PRINT_PACKET_CURRENT(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),rtsp_print_packet_current(b),printf("]\n"),\
                            printf(RESET))

    #define RTSP_PRINT_PACKET(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),rtsp_print_packet(b),printf("]\n"),\
                            printf(RESET))
    #define RTSP_PRINT_TIMEVAL(a,b) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KGREEN),\
                            printf(" %s", a), \
                             rtsp_print_timeval(b),\
                             printf(RESET))
#else
    #define RTSP_PRINT(...)
    #define RTSP_PRINT_DETAIL(...)
    #define RTSP_PRINT_DBG(...)
    #define RTSP_PRINT_FATAL(...)
    #define RTSP_PRINT_EMERG(...)
    #define RTSP_PRINT_ALERT(...)
    #define RTSP_PRINT_CRIT(...)
    #define RTSP_PRINT_ERR(...)
    #define RTSP_PRINT_WARN(...)
    #define RTSP_PRINT_NOTICE(...)
    #define RTSP_PRINT_INFO(...)
    #define PRINT_HEX_STRING(a,b,c)
    #define RTSP_PRINT_PACKET_SPECIFIED(tag, packet, offset, len)
    #define RTSP_PRINT_PACKET_CURRENT(a,b)
    #define RTSP_PRINT_PACKET(a,b)
    #define RTSP_PRINT_TIMEVAL(a,b)
#endif

#if APM_RTSP_ENABLE_FRLOG
    #define RTSP_FRLOG_ERROR(...) FRLOG_ERROR_DETAIL(...)
#else
#define RTSP_FRLOG_ERROR(...)
#endif

#define RTSP_MALLOCK_ERR RTSP_PRINT_EMERG("can not malloc memory\n"); \
                          FRLOG_ERROR_DETAIL("can not malloc memory");

void rtsp_free(void **pp_buffer);

void rtsp_print_timeval(struct timeval st_timestamp);

void rtsp_bin_to_hex_string(char *p_inbuff
                            , int i_len
                            , char *p_outbuff);

void rtsp_print_hex_string(char *p_buff
                        , int i_len);

void rtsp_print_packet_specified(L7_rtsp_packet *p_st_rtsp_packet
                                , int i_pos
                                , int i_len);

void rtsp_print_packet_current(L7_rtsp_packet *p_st_rtsp_packet);

void rtsp_print_packet(L7_rtsp_packet *p_st_rtsp_packet);

int rtsp_packet_is_in_fragment(L7_rtsp_packet *p_st_rtsp_packet);

int rtsp_report_data(Conntrack *p_st_conn
                    , int i_is_req
                    , Frreport_body_data *p_st_body
                    , Frhash_header *p_st_hash_report);

void rtsp_free_stmt(L7_rtsp_stmt **pp_st_rtsp_stmt);

void rtsp_init_stmt(L7_rtsp_stmt *p_st_rtsp_stmt);

#endif
